<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>编辑计划</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="./lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="./css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <!-- 隐藏ID -->
    <input type="hidden" name="id">

    <!-- 计划类型 -->
    <div class="layui-form-item">
        <label class="layui-form-label required">计划类型</label>
        <div class="layui-input-block">
            <select name="type" lay-filter="type">
                <option value="配变电">配变电</option>
                <option value="线路">线路</option>
            </select>
        </div>
    </div>

    <!-- 资产编号 -->
    <div class="layui-form-item">
        <label class="layui-form-label required">资产编号</label>
        <div class="layui-input-block">
            <input type="text" name="propertyNumber" lay-verify="required" placeholder="请输入资产编号"
                   autocomplete="off" class="layui-input">
        </div>
    </div>

    <!-- 项目编号 -->
    <div class="layui-form-item">
        <label class="layui-form-label required">项目编号</label>
        <div class="layui-input-block">
            <input type="text" name="projectNumber" lay-verify="required" placeholder="请输入项目编号"
                   autocomplete="off" class="layui-input">
        </div>
    </div>

    <!-- 项目名称 -->
    <div class="layui-form-item">
        <label class="layui-form-label required">项目名称</label>
        <div class="layui-input-block">
            <input type="text" name="projectName" lay-verify="required" placeholder="请输入项目名称"
                   autocomplete="off" class="layui-input">
        </div>
    </div>

    <!-- 废旧物料描述 -->
    <div class="layui-form-item">
        <label class="layui-form-label required">废旧物料描述</label>
        <div class="layui-input-block">
            <input type="text" name="materialsDescription" lay-verify="required" placeholder="请输入废旧物料描述"
                   autocomplete="off" class="layui-input">
        </div>
    </div>

    <!-- 计划完成时间 -->
    <div class="layui-form-item">
        <label class="layui-form-label required">计划完成时间</label>
        <div class="layui-input-block">
            <input type="date" name="finishTime" lay-verify="required" class="layui-input">
        </div>
    </div>

    <!-- 数量 -->
    <div class="layui-form-item">
        <label class="layui-form-label required">数量</label>
        <div class="layui-input-block">
            <input type="number" step="0.01" name="quantity" lay-verify="required" placeholder="请输入数量"
                   autocomplete="off" class="layui-input">
        </div>
    </div>

    <!-- 状态 -->
    <div class="layui-form-item">
        <label class="layui-form-label required">状态</label>
        <div class="layui-input-block">
            <select name="status" lay-filter="status">
                <option value="0">未完成</option>
                <option value="1">已完成</option>
            </select>
        </div>
    </div>

    <!-- 提交按钮 -->
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>

<script src="./lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>


    layui.use(['form', 'layer', 'jquery'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.jquery;

        window.loadData = function(data) {
            $("input[name='id']").val(data.id);
            $("input[name='propertyNumber']").val(data.propertyNumber);
            $("input[name='projectNumber']").val(data.projectNumber);
            $("input[name='projectName']").val(data.projectName);
            $("input[name='materialsDescription']").val(data.materialsDescription);
            $("input[name='finishTime']").val(new Date(data.finishTime).toISOString().split('T')[0]);
            $("input[name='quantity']").val(data.quantity);
            $("select[name='status']").val(data.status);

            // 设置计划类型下拉框
            $("select[name='type']").val(data.type);

            // 渲染下拉框
            layui.form.render("select");
        };
        // 监听提交
        form.on('submit(saveBtn)', function (data) {
            var loadingIndex = layer.load(2); // 加载动画

            $.ajax({
                url: '/plan/normal/update',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(data.field),
                success: function (res) {
                    layer.close(loadingIndex);
                    if (res.code === 0) {
                        layer.msg('更新成功');

                        //页面持续几秒钟再关闭


                        // 关闭弹出层并刷新表格
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                        parent.location.reload();
                    } else {
                        layer.msg('更新失败：' + res.msg);
                    }
                },
                error: function () {
                    layer.close(loadingIndex);
                    layer.msg('网络异常，请稍后再试');
                }
            });

            return false; // 阻止表单跳转
        });
    });
</script>
</body>
</html>
